package com.mao;

public class ArrayTest6 {
    public static void main(String[] args) {
        // 目标：完成数字华容道的初始化和随机乱序
        start(4);
    }
    public static void start(int n)
    {
        //定义一个二维数组存储数字列表
        int[][] arr = new int[n][n];

        //遍历二维数组，填充数字列表
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = count++;
            }
        }
        printArr(arr);
        System.out.println("================");
        //随机乱序
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                int r = (int) (Math.random()*arr.length);
                int c = (int) (Math.random()*arr[i].length);
                int temp = arr[i][j];
                arr[i][j] = arr[r][c];
                arr[r][c] = temp;
            }
        }
        printArr(arr);
    }
    //打印二维数组
    public static void printArr(int[][] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
